Sending Mails with SMTP

This lesson explains how Go code can be used to send an email using an SMTP server.

We'll cover the following

Overview of SMTP in Go#

The package net/smtp implements the Simple Mail Transfer Protocol for sending mail. It contains a Client type that represents a client connection to an SMTP server:

  • Dial returns a new Client connected to an SMTP server.
  • Set Mail (=from) and Rcpt(= to)
  • Data returns a writer that can be used to write the data, here with buf.WriteTo(wc).
svg viewer

Explanation#

SMTP Server

In the code above, we need the package net/smtp, which is imported at line 5. First, we need to connect to an active remote SMTP server, which is done with the Dial method at line 10, creating a client instance. Error-handling (from line 11 to line 13) exits the program on error.

We set up the sender and receiver email address at line 15 and line 16, respectively. Line 18 constructs the Data method on client, which makes a client writer wc with similar error-handling from line 19 to line 21. At line 22, we make sure that the writer wc will be closed. Then, at line 23, we make a buffered string and write it to wc at line 24. This if-statement is combined with error-handling, logging an eventual error and exiting the program at line 25.

The function SendMail can be used if authentication is needed and when you have a number of recipients. It connects to the server at addr, switches to TLS (Transport Layer Security encryption and authentication protocol), authenticates with the mechanism if possible, and then sends an email from address from to addresses to, with the message msg:

func SendMail(addr string, a Auth, from string, to []string, msg []byte) error

Go through the following illustrations that explain how to set a Gmail account for sending an email before running a program.

Created with Fabric.js 1.6.0-rc.1 Click on Security
Step 1: Open your Account Settings

1 of 7

Created with Fabric.js 3.6.2 Turn 2-Step Verification on.If already on move to the next step
Step 2: Enable 2-Step Verification

2 of 7

Created with Fabric.js 3.6.2 Click App passwords to generate a passwordIf already generated then use the generated password.If you don't remember it make a new one.
Step 3: Generate an App Password

3 of 7

Created with Fabric.js 3.6.2 After the verification, the following page will open
Step3: Generate App Password

4 of 7

Created with Fabric.js 1.6.0-rc.1 Choose Other option and give any name by your own choice
Step3: Generate App Password

5 of 7

Created with Fabric.js 1.6.0-rc.1 Choose your device from the pop-upIf your device isn't listed then select Otherand name you device After selecting Device and App click Generate
Step3: Generate App Password

6 of 7

Created with Fabric.js 1.6.0-rc.1 Don't forget to copy this 16 digit password.You can use this password in the program to send email rather than using original password.
Step 4: Copy the Password

7 of 7

Look at the following program to see how it works:

This code requires the following environment variables to execute:
GOROOT
/usr/local/go
GOPATH
//root/usr/local/go/src
PATH
//root/usr/local/go/src/bin:/usr/local/go/bin://root/usr/local/go/src/bin:/usr/local/go/bin://root/usr/local/go/src/bin:/usr/local/go/bin://root/usr/local/go/src/bin:/usr/local/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
/
main.go

Click the RUN button and wait for the terminal to start. Type go run main.go in the terminal and press ENTER.


Hurrah! You just sent your first email with Go! Cloud computing is so popular nowadays, and Go provides support for it. See the next lesson to see how Golang has made this possible.

Remote Procedure Calls with RPC

Go in the Cloud